package controller;

import beans.Car;
import beans.Good;
import utils.JDBCUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;


public class AddCarController extends HttpServlet {
    private static final long serialVersionUID = 7804524886360637172L;

    public AddCarController() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            request.setCharacterEncoding("UTF-8");
            response.setCharacterEncoding("UTF-8");

            HttpSession session = request.getSession();
            String goods_id = request.getParameter("goods_id");
            Object user_id = session.getAttribute("user_id");

            // 查询商品
            String sql = "SELECT * FROM goods WHERE goods_id=?";
            Connection connection = JDBCUtils.getConnection();
            PreparedStatement stmt = connection.prepareStatement(sql);
            stmt.setString(1, goods_id);
            ResultSet rs = stmt.executeQuery();
            Good good = new Good();
            if (rs.next()) {
                good.setGoods_id(rs.getString("goods_id"));
                good.setGoods_name(rs.getString("goods_name"));
                good.setUnitprice(rs.getString("unitprice"));
                good.setDetails(rs.getString("details"));
                good.setPic(rs.getString("photo"));
            }

            // 检查购物车中是否已经有这个商品
            String checkSql = "SELECT quantity FROM car WHERE goods_id=? AND userid=?";
            stmt = connection.prepareStatement(checkSql);
            stmt.setString(1, goods_id);
            stmt.setString(2, user_id.toString());
            rs = stmt.executeQuery();
            int quantity = 1; // 默认添加数量为1
            if (rs.next()) {
                quantity = rs.getInt("quantity") + 1; // 如果商品已存在，增加数量
            }

            // 添加或更新购物车
            String sqladd = "INSERT INTO car (goods_id, goods_name, unitprice, quantity, userid, pic) VALUES (?,?,?,?,?,?) ON DUPLICATE KEY UPDATE quantity=quantity+1";
            stmt = connection.prepareStatement(sqladd);
            stmt.setString(1, good.getGoods_id());
            stmt.setString(2, good.getGoods_name());
            stmt.setString(3, good.getUnitprice());
            stmt.setInt(4, quantity);
            stmt.setString(5, user_id.toString());
            stmt.setString(6, good.getPic());
            int res = stmt.executeUpdate();
            if (res > 0) {
                System.out.println("插入或更新成功");
            }

            // 查询购物车并设置到session
            String carsql = "SELECT * FROM car WHERE userid=?";
            stmt = connection.prepareStatement(carsql);
            stmt.setString(1, user_id.toString());
            rs = stmt.executeQuery();
            List<Car> carList = new ArrayList<>();
            Car car;
            while (rs.next()) {
                car = new Car();
                car.setGoods_id(rs.getString("goods_id"));
                car.setGoods_name(rs.getString("goods_name"));
                car.setUnitprice(rs.getString("unitprice"));
                car.setQuantity(rs.getInt("quantity") + ""); // 确保是字符串类型
                car.setPic(rs.getString("pic"));
                carList.add(car);
            }
            connection.close();
            request.getSession().setAttribute("carList", carList);
            response.sendRedirect("car.jsp");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}